import java.util.Arrays;

public class Solution {
    public int[] advantageCount(int[] nums1, int[] nums2) {
        int len = nums1.length;

        //nums2元素的下标数组
        Integer[] index = new Integer[len];

        //初始化index数组
        for (int i = 0; i < len; i++) {
            index[i] = i;
        }

        Arrays.sort(nums1);
        Arrays.sort(index, (a, b) -> {
            return nums2[a] - nums2[b];
        });

        int[] ret = new int[len];
        int left = 0, right = len - 1;
        for (int i = 0; i < len; i++) {
            if (nums1[i] <= nums2[index[left]]) {
                ret[index[right--]] = nums1[i];
            } else {
                ret[index[left++]] = nums1[i];
            }
        }

        return ret;
    }
}
